This is a markdown template to produce HTML files. The template is a part of the biometrics package developed by Paul Stevenson at Telethon Kids Institute. The template is built on bootstrap and the associated formatting tags should be available.
The markdown RStudio cheat sheat is here. Various information can be found here, including shortcuts to modify the text. Bootstrap typography codes also seem to be available to format markdown HTML documents; this feature has not been thoroughly tested by the author.
Referencing important previous work and the scientific principles that underpin research is a vital aspect of any report. The default bibliography list that is used by this template is found in assets/bib/bibliography.bib. The author has included these references in the document for illustrative purposes (2017); however, they have nothing to do with anything that is written here (R Core Team 2021; Spinu, Grolemund, and Wickham 2021).
Bibliographic information for R packages are automatically generated (and loaded) with LoadandCite (Gandrud 2016). By default, the package bibliography is stored in assets/bib/packages.bib, which is overwritten on each Knit and should not be manually altered.
References are automatically placed at the end of the document, so it is important that a header, such as # References, is placed at the end of your markdown document.
R chunks are added through the following blocks of code:
```{r chunk_name, include = FALSE}
print("Hello World!")
```
The above code produces the following in the markdown document:
print("Hello World!")## [1] "Hello World!"
The echo = FALSE argument can be used if you do not want the source code to be displayed:
## [1] "In this example, the source code has been hidden and only the output is displayed."
Output can be suppressed entirely by changing include = FALSE, or by removing the argument.
This template is built with the ProjectTemplate package in mind. By default, all data in the “cache” directories will be loaded. Files in the data directory are note loaded by default, but can be by setting data_loading = TRUE in the initialisation markdown chunk. Similarly, by default munge = FALSE and scripts in the munge directory will not be executed.
R’s included Iris data set will be used to illustrate how to include graphics and to use the colour palettes defined at the start of the document:
data(iris)Kable is a very simple table generator that takes input as either a matrix or data frame (Zhu 2021); see the Kable documentation for styling options available.
In this template the kable formatting is applied automatically when using the chunk option table = TRUE. By default, this template uses the style with hover
full_width = FALSE. (Technical info: automatic formatting is implemented by kintr::knit_hooks() (Xie 2021) via the chunk source and optimal results are achieved when including the option results = . For the same reason, hide
eval = FALSE is included so that the chunk is not evaluated twice. The source code cannot be shown when the kable formatting is automatically applied.)
For example:
```{r, table = TRUE, results = "hide", eval = FALSE}
head(iris)
```
will output:
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
If custom table options are needed, then the kableExtra formatting can be applied as normal:
head(iris) %>%
kable("html", escape = FALSE) %>%
kable_styling("hover", full_width = FALSE)| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
broom::tidy()The broom::tidy() (Robinson, Hayes, and Couch 2021) function may be used to convert the output into a data frame that is compatible with kable When data is supplied from a model, which may be more appropraite for a report or publication.
For example, the summary output of a linear model looks like:
model <- lm(Sepal.Length ~ Sepal.Width + Petal.Width, data = iris)
summary(model)
Call:
lm(formula = Sepal.Length ~ Sepal.Width + Petal.Width, data = iris)
Residuals:
Min 1Q Median 3Q Max
-1.2076 -0.2288 -0.0450 0.2266 1.1810
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.45733 0.30919 11.18 < 2e-16 ***
Sepal.Width 0.39907 0.09111 4.38 2.24e-05 ***
Petal.Width 0.97213 0.05210 18.66 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4511 on 147 degrees of freedom
Multiple R-squared: 0.7072, Adjusted R-squared: 0.7033
F-statistic: 177.6 on 2 and 147 DF, p-value: < 2.2e-16
Broom defines tidying methods for extracting three kinds of statistics from an object (see here):
tidy(): component-level statisticsglance(): model-level statisticsaugment(): observation-level statistics| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 3.4573334 | 0.3091878 | 11.181987 | 0.00e+00 |
| Sepal.Width | 0.3990708 | 0.0911096 | 4.380117 | 2.24e-05 |
| Petal.Width | 0.9721296 | 0.0520987 | 18.659374 | 0.00e+00 |
| r.squared | adj.r.squared | sigma | statistic | p.value | df | logLik | AIC | BIC | deviance | df.residual | nobs |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0.7072371 | 0.7032539 | 0.4510841 | 177.5564 | 0 | 2 | -91.91036 | 191.8207 | 203.8633 | 29.9111 | 147 | 150 |
| Sepal.Length | Sepal.Width | Petal.Width | .fitted | .resid | .hat | .sigma | .cooksd | .std.resid |
|---|---|---|---|---|---|---|---|---|
| 5.1 | 3.5 | 0.2 | 5.048507 | 0.0514929 | 0.0204259 | 0.4526058 | 0.0000925 | 0.1153376 |
| 4.9 | 3.0 | 0.2 | 4.848972 | 0.0510283 | 0.0211012 | 0.4526062 | 0.0000939 | 0.1143364 |
| 4.7 | 3.2 | 0.2 | 4.928786 | -0.2287859 | 0.0183833 | 0.4522226 | 0.0016359 | -0.5119184 |
| 4.6 | 3.1 | 0.2 | 4.888879 | -0.2888788 | 0.0193343 | 0.4519820 | 0.0027484 | -0.6466922 |
| 5.0 | 3.6 | 0.2 | 5.088414 | -0.0884142 | 0.0227385 | 0.4525658 | 0.0003049 | -0.1982709 |
| 5.4 | 3.9 | 0.4 | 5.402561 | -0.0025614 | 0.0326523 | 0.4526262 | 0.0000004 | -0.0057733 |
Two of the functions in the jtools (Long 2021) package are useful to produce tables and figures of model output.
models <- lapply(c("setosa", "versicolor", "virginica"),
function(x) lm(Sepal.Length ~ Sepal.Width + Petal.Width, data = iris, subset = Species == x))
names(models) <- c("setosa", "versicolor", "virginica")
jtools::export_summs(models,
error_format = "CI [{conf.low}, {conf.high}]",
exp = FALSE)| setosa | versicolor | virginica | |
|---|---|---|---|
| (Intercept) | 2.63 *** | 3.39 *** | 3.83 *** |
| CI [2.01, 3.25] | CI [2.29, 4.48] | CI [2.22, 5.44] | |
| Sepal.Width | 0.67 *** | 0.48 | 0.85 ** |
| CI [0.48, 0.85] | CI [-0.04, 1.00] | CI [0.24, 1.46] | |
| Petal.Width | 0.37 | 0.92 * | 0.11 |
| CI [-0.29, 1.04] | CI [0.09, 1.75] | CI [-0.60, 0.83] | |
| N | 50 | 50 | 50 |
| R2 | 0.56 | 0.35 | 0.21 |
| *** p < 0.001; ** p < 0.01; * p < 0.05. | |||
do.call(jtools::plot_summs,
c(unname(models),
list(model.names = names(models),
exp = FALSE,
colors = unname(telethonkids_cols()[c(2, 4, 8)])))) +
labs(title = "Example of plot_summs", subtitle = "Input is a list of models")These will work for many different types of models.
The GGally::ggpairs() function may be used to produce a visual summary of the input data:
ggpairs(iris,
mapping = ggplot2::aes(colour = Species),
diag = list(continuous = "densityDiag"),
axisLabels = "show")There are three colour schemes built into this theme: scale_colour_telethonkids(, light
)scale_colour_telethonkids( and dark
)scale_colour_telethonkids(. These colour themes are implemented by adding the appropriate theme to the ggplot code:grey
)
ggplot(iris, aes(Petal.Width, Petal.Length, color = Species)) +
labs(title = "Iris Dataset", x = "Petal Width", y = "Petal Length") +
geom_point(size = 4) +
scale_color_telethonkids("light") +
theme_classic()A continuous colour theme can be implemented by adding the argument discrete = FALSE the scale_colour_telethonkids():
ggplot(iris, aes(Petal.Width, Petal.Length, color = Petal.Length)) +
geom_point(size = 4, alpha = .6) +
scale_color_telethonkids(discrete = FALSE, palette = "dark") +
theme_classic()Bar plots can also be ‘filled’ by discrete variables with differnt palettes by using scale_fill_telethonkids(palette = .mixed
, guide = none
)
To ensure the image is responsive on different sized screens, the code out.extra = should be included in the R chunk options (this calls an option hook defined in figure
assets/R/hooks.R).
library(captioner)figures <- captioner(prefix = "Figure")
tables <- captioner(prefix = "Table")These should listed in the deired sequential order of the text
tables(name = "tab1", caption = "Caption")
figures(name = "fig1", caption = "Caption.")Full caption: tables(tab1
)
In-text reference (e.g. “Table 1”): tables(tab1
, display = cite
)
Caption number only (e.g. “1”): tables(tab1
, display = num
)